package com.dz.lm.web.controller.newuser.userinvinte;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.utils.DateUtil;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.dao.user.read.UserDao;
import com.dz.lm.dao.userinvite.write.UserInviteDao;
import com.dz.lm.domain.userinvite.UserInvite;
import com.dz.lm.domain.userinvite.UserInviteExample;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * 推广码
 */
@RestController
@RequestMapping(value = "/h5/weijie/rewriteUrl/userInvinte", produces = "application/json")
public class UserInvinteCotroller extends BasicControl {


    @Resource(name = "writeUserInviteDao")
    private UserInviteDao writeUserInviteDao;
    @Resource(name ="readUserInviteDao" )
    private com.dz.lm.dao.userinvite.read.UserInviteDao  readUserInviteDao;
    @Resource(name = "readUserDao")
    private UserDao readUserDao;
    /**
     *
     *推广码填写
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/addCode")
    public Map addCode(HttpServletRequest request) throws Exception {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //用户
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //推广码
        String code = param.get("code") != null ? param.get("code").toString() : "";
        Map map = new HashMap();
        map.put("promoCode",code);
        Map user = readUserDao.qryUserPromoCode(map);
        if(user != null){
            UserInvite invite = new UserInvite();
            invite.setCreateTime( DateUtil.getTimeStr( new Date()));
            invite.setInviteUid(Integer.valueOf(user.get("userId").toString()));
            invite.setUserId(userId);
            invite.setCode(code);
            writeUserInviteDao.insertSelective(invite);
            result ="00";
            message="操作成功";
        }else {
            message ="推广码有误";
        }

        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     *
     *推广码次数查询
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/queryCount")
    public Map queryCount(HttpServletRequest request) throws Exception {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();


        //用户
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        UserInviteExample ex = new UserInviteExample();
        UserInviteExample.Criteria cr = ex.createCriteria();
        cr.andInviteUidEqualTo(userId);
        int count = readUserInviteDao.queryCount(ex);
        Map map = new HashMap();
        map.put("count",count);
        data=map;
        message="获取成功";
        result="00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
}
